/**
 ****************************************************************************************
 *
 * @file arch_main.c
 *
 * @brief Main loop of the application.
 *
 * Copyright (C) Beken Corp 2011-2020
 *
 ****************************************************************************************
 */
#include "include.h"
#include "driver_pub.h"
#include "func_pub.h"
#include "app.h"
#include "ate_app.h"
#include "start_type_pub.h"
#include "bk7011_cal_pub.h"
#include "reg_mdm_cfg.h"
#include "uart.h"
#include "arm_arch.h"
#include "gpio_if.h"
#include "uart_if.h"
#include "ate_app.h"
#include "pwm_if.h"
static DevHandle uart_handle;
static DevHandle pwm_handle;
static DevHandle wdt_handle;

#if CFG_SUPPORT_LITEOS
#include "los_context.h"
#include "los_task.h"
#endif

beken_semaphore_t extended_app_sema = NULL;
uint32_t  extended_app_stack_size = 2048;

void extended_app_launch_over(void)
{  
    OSStatus ret;
    ret = rtos_set_semaphore(&extended_app_sema);

    (void)ret;
}
    
void extended_app_waiting_for_launch(void)
{
    OSStatus ret;

    ret = rtos_get_semaphore(&extended_app_sema, BEKEN_WAIT_FOREVER);
    ASSERT(kNoErr == ret);

    (void)ret;
}

void improve_rx_sensitivity(void)
{
#if (CFG_SOC_NAME == SOC_BK7231N)
    /* set TRX_REG12<8:7>=2 for rx */
    rwnx_cal_set_reg_adda_ldo(2);
    /* set MDM_REG202<23>=1 for rx */
        mdm_cpemode_setf(1);
    /* set MDM_REG206<17:16>=3 for band20 */
    mdm_cfgsmooth_setf(3);
    rwnx_cal_dis_rx_filter_offset();

    if(!get_ate_mode_state())
        bk7011_reduce_vdddig_for_rx(1);
#endif
}

static void extended_app_task_handler(void *arg)
{
    /* step 0: function layer initialization */

    func_init_extended();  

    /* step 1: startup application layer */

    if (get_ate_mode_state()) {
        ate_start();
        improve_rx_sensitivity();
    }
    else {
        app_start();
    }

    extended_app_launch_over();

    rtos_delete_thread( NULL );
}

void extended_app_init(void)
{
    OSStatus ret;

    ret = rtos_init_semaphore(&extended_app_sema, 1);	
    ASSERT(kNoErr == ret);
}

void extended_app_uninit(void)
{
    OSStatus ret;

    ret = rtos_deinit_semaphore(&extended_app_sema);	
    ASSERT(kNoErr == ret);
}

void extended_app_launch(void)
{
    OSStatus ret;

    ret = rtos_create_thread(NULL,
        THD_EXTENDED_APP_PRIORITY,
        "extended_app",
        (beken_thread_function_t)\
        extended_app_task_handler,
        extended_app_stack_size,
        (beken_thread_arg_t)0);
    ASSERT(kNoErr == ret);
}
void gpio_test(void)
{
    int ret = 0;

    ret = GpioSetDir(GPIO7, GPIO_DIR_OUT);
    if (ret != 0) {
        printf("GpioSetDir: failed!\n");
    }
    ret = GpioWrite(GPIO7, GPIO_INT_LEVEL_LOW);
    if (ret != 0) {
        printf("GpioWrite: failed!\n");
    }

    ret = GpioSetDir(GPIO8, GPIO_DIR_OUT);
    if (ret != 0) {
        printf("GpioSetDir: failed!\n");
    }
    ret = GpioWrite(GPIO8, GPIO_INT_LEVEL_HIGH);
    if (ret != 0) {
        printf("GpioWrite: failed!\n");
    }
}

void uart_test(void)
{
    int ret = 0;
    uint32_t baudrate;
    struct UartAttribute attribute;
    uint32_t set_baudrate = 115200;
    uint8_t sbuff[4] = {'a', 'b', 'c', 'd'};

    uart_handle = UartOpen(0);
    if (uart_handle == NULL) {
        printf("open uart fail\n");
    }

    ret = UartSetBaud(uart_handle, set_baudrate);
    if (ret != 0) {
        printf("UartSetBaud: failed, ret %d\n", ret);
    }

    ret = UartGetBaud(uart_handle, &baudrate);
    if (ret != 0) {
        printf("UartGetBaud: failed, ret %d\n", ret);
    }

    printf("get baudrate = %d\r\n", baudrate);

    ret = UartSetAttribute(uart_handle, &attribute);
    if (ret != 0) {
        printf("UartSetAttribute: failed, ret %d\n", ret);
    }

    ret = UartWrite(uart_handle, sbuff, 4);
    if (ret != 0) {
        printf("UartWrite: failed, ret %d\n", ret);
    }
}

void i2c_test(void)
{
    int ret = 0;
    DevHandle i2c;

    i2c = I2cOpen(0);
    if (i2c  == NULL) {
        printf("open i2c fail\n");
    }
    ret = I2cTransfer(i2c, 0, 1);
    if (ret != 0) {
        printf("I2cTrans: failed, ret %d\n", ret);
    }
}

void pwm_test(void)
{
    int ret = 0;
    struct PwmConfig config;
    uint32_t pwm_duty = 500;
    uint32_t pwm_period = 1000;

    config.duty = pwm_duty;
    config.period = pwm_period;
    config.polarity = PWM_INVERTED_POLARITY;

    pwm_handle = PwmOpen(0);
    if (pwm_handle  == NULL) {
        printf("open pwm fail\n");
    }
    PwmSetConfig(pwm_handle, &config);
    PwmClose(pwm_handle);
}

void watchdog_test(void)
{
    int ret = 0;
    uint32_t timeoutGet = 0;
    const uint32_t timeoutSet = 20;

    if (WatchdogOpen(0, &wdt_handle) != HDF_SUCCESS) {
        printf("Open watchdog fail!\n");
    }

    ret = WatchdogStart(wdt_handle);
    if (ret != HDF_SUCCESS) {
        printf("%s: satrt fail! ret:%d\n", __func__, ret);
        WatchdogClose(wdt_handle);
    }

    /* ret = WatchdogGetTimeout(wdt_handle, &timeoutGet);
    if (ret != HDF_SUCCESS) {
        printf("%s: get timeout fail! ret:%d\n", __func__, ret);
        return;
    } */

    ret = WatchdogSetTimeout(wdt_handle, timeoutSet);
    if (ret != HDF_SUCCESS) {
        printf("%s: set timeout fail! ret:%d\n", __func__, ret);
    }
}

void driver_test(void)
{
    gpio_test();
    uart_test();
    i2c_test();
    pwm_test();
    watchdog_test();
}

void entry_main(void)
{
    uart_fast_init();

    arm9_enable_alignfault();

    ate_app_init();

    bk_misc_init_start_type();

    LOS_KernelInit();

    /* step 1: driver layer initialization*/
    driver_init();

    bk_misc_check_start_type();
    func_init_basic();

    extended_app_init();

    /* step 2: user entry
        attention: if the user wants to increase boot time and light up the board, you may invoke the
       			  routine:user_main_entry before invoking extended_app_launch; and then user MUST wait 
       			  for the init of most other devices, such as:wlan and so on;
       			  
       			  if you do not care the boot time, the function: extended_app_launch shall be invoked 
       			  BEFORE user_main_entry;*/
    //RunTaskSample();    //for kernel task test

    //ble_test_main();    //for ble test

    OHOS_SystemInit();

    if (DeviceManagerStart()) {
          printf("[%s] No drivers need load by hdf manager!",__func__);
        }else {
          printf("hdf success!\n");
        }

    driver_test();

    LOS_Start();
}
// eof
